<?php
declare (strict_types = 1);

namespace app\admin\service\data\transaction;

use app\admin\model\data\ClassesOrder;
use app\admin\model\data\Course;
use app\admin\model\data\LiveOrder;
use app\admin\model\data\LiveSmallOrder;
use app\admin\model\data\PaymentOrderItem;
use app\admin\model\data\UserPaymentData;
use app\admin\model\data\VideoOrder;
use app\admin\service\data\DataService;

/**
 * 数据中心-交易分析
 */
class Transaction extends DataService
{
    /**
     * 课程交易分析
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-28
     * @return   [type]                         [description]
     */
    public function course()
    {
        $daterange   = $this->parseSelectDaterange();
        $data        = [];
        $timespan    = $daterange['timespan'];
        $product_ids = Course::getCourseProducts();
        // 下单数
        $data['place_order_count'] = PaymentOrderItem::scope('mhm')->whereBetween('create_time', $timespan)->whereIn('product_type_id', $product_ids)->count();
        // 支付订单数
        $data['payment_order_count'] = PaymentOrderItem::scope('mhm', '__TABLE__.mhm_id')->withJoin(['pdorder' => ['status']])->whereBetween('__TABLE__.create_time', $timespan)->whereIn('pdorder.status', [20, 30, 40])->whereIn('__TABLE__.product_type_id', $product_ids)->count();
        // 下单用户数
        $data['place_user_count'] = PaymentOrderItem::scope('mhm')->whereBetween('create_time', $timespan)->whereIn('product_type_id', $product_ids)->count('DISTINCT user_id');
        // 支付用户数
        $data['payment_user_count'] = PaymentOrderItem::scope('mhm', '__TABLE__.mhm_id')->withJoin(['pdorder' => ['status', 'payment_time']])->whereBetween('__TABLE__.create_time', $timespan)->whereIn('pdorder.status', [20, 30, 40])->whereIn('payment_order_item.product_type_id', $product_ids)->value('COUNT(DISTINCT payment_order_item.user_id)');
        // 下单金额
        $data['place_price'] = (float) PaymentOrderItem::scope('mhm')->whereBetween('create_time', $timespan)->whereIn('product_type_id', $product_ids)->sum('payment');
        // 支付金额
        $data['payment'] = (float) PaymentOrderItem::scope('mhm', '__TABLE__.mhm_id')->withJoin(['pdorder' => ['status', 'payment_time']])->whereBetween('__TABLE__.create_time', $timespan)->whereIn('pdorder.status', [20, 30, 40])->whereIn('payment_order_item.product_type_id', $product_ids)->sum('payment_order_item.payment');
        // 图表数据 - 订单量
        $data['order_count_rows'] = $this->paddingOrderCount($daterange);
        // 图表数据 - 订单支付金额
        $data['payment_rows'] = $this->paddingPayment($daterange);
        // 付费新用户数量以及金额
        $new                      = UserPaymentData::scope('mhm', 'user.mhm_id')->withJoin(['user' => ['mhm_id']])->whereBetween('__TABLE__.create_time', $timespan)->field(['SUM(payment)' => 'tp_payment', 'COUNT(1)' => 'tp_count'])->find();
        $data['new_user_count']   = $new['tp_count'];
        $data['new_user_payment'] = $new['tp_payment'];
        // 付费老用户数量以及金额 = 总和 - 新用户的
        // 付费总金额 = 这段时间下单量付费的总金额 = 上面查出的支付金额
        $total                    = $data['payment'];
        $data['old_user_payment'] = $total - $data['new_user_payment'];
        // 付费总人数 = 这段时间下单人数中已经付费的 = 上面的支付用户数
        $total                  = $data['payment_user_count'];
        $data['old_user_count'] = $total - $data['new_user_count'];
        // 用户占比
        $data['new_user_count_rate'] = $this->getProportionRate($total, $data['new_user_count'], 0);
        $data['old_user_count_rate'] = $total == 0 ? 0 : round(100 - $data['new_user_count_rate'],2);
        $data['payment_rate']        = [[
            'title' => '老用户付款人数',
            'value' => $data['old_user_count'],
        ],
            [
                'title' => '新用户付款人数',
                'value' => $data['new_user_count'],
            ],
        ];

        $data['is_platform'] = request()->is_platform;

        return $this->parseToData($data, 1);
    }

    /**
     * 支付订单量统计图表数据
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-28
     * @param    [type]                         $daterange [description]
     * @return   [type]                                    [description]
     */
    protected function paddingOrderCount($daterange)
    {
        $timespan   = $daterange['timespan'];
        $video      = VideoOrder::scope('mhm', '__TABLE__.mhm_id')->withJoin(['pdorder' => ['pdorder.status']])->whereIn('pdorder.status', [20, 30, 40])->whereBetween('__TABLE__.create_time', $timespan)->count();
        $live       = LiveOrder::scope('mhm', '__TABLE__.mhm_id')->withJoin(['pdorder' => ['pdorder.status']])->whereIn('pdorder.status', [20, 30, 40])->whereBetween('__TABLE__.create_time', $timespan)->count();
        $live_small = LiveSmallOrder::scope('mhm', '__TABLE__.mhm_id')->withJoin(['pdorder' => ['pdorder.status']])->whereIn('pdorder.status', [20, 30, 40])->whereBetween('__TABLE__.create_time', $timespan)->count();
        $classes    = ClassesOrder::scope('mhm', '__TABLE__.mhm_id')->withJoin(['pdorder' => ['pdorder.status']])->whereIn('pdorder.status', [20, 30, 40])->whereBetween('__TABLE__.create_time', $timespan)->count();
        $rows       = [];
        if ($video || $live || $live_small || $classes) {
            $rows = [
                ['order_type' => '点播课', 'order_count' => $video],
                ['order_type' => '大班课', 'order_count' => $live],
                ['order_type' => '小班课', 'order_count' => $live_small],
                ['order_type' => '班级课', 'order_count' => $classes],
            ];
        }

        return $rows;
    }

    /**
     * 支付金额统计图表数据
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-28
     * @param    [type]                         $daterange [description]
     * @return   [type]                                    [description]
     */
    protected function paddingPayment($daterange)
    {
        $timespan   = $daterange['timespan'];
        $video      = VideoOrder::scope('mhm', '__TABLE__.mhm_id')->withJoin(['pdorder' => ['pdorder.status']])->whereIn('pdorder.status', [20, 30, 40])->whereBetween('__TABLE__.create_time', $timespan)->sum('__TABLE__.payment');
        $live       = LiveOrder::scope('mhm', '__TABLE__.mhm_id')->withJoin(['pdorder' => ['pdorder.status']])->whereIn('pdorder.status', [20, 30, 40])->whereBetween('__TABLE__.create_time', $timespan)->sum('__TABLE__.payment');
        $live_small = LiveSmallOrder::scope('mhm', '__TABLE__.mhm_id')->withJoin(['pdorder' => ['pdorder.status']])->whereIn('pdorder.status', [20, 30, 40])->whereBetween('__TABLE__.create_time', $timespan)->sum('__TABLE__.payment');
        $classes    = ClassesOrder::scope('mhm', '__TABLE__.mhm_id')->withJoin(['pdorder' => ['pdorder.status']])->whereIn('pdorder.status', [20, 30, 40])->whereBetween('__TABLE__.create_time', $timespan)->sum('__TABLE__.payment');
        $rows       = [];
        if ($video || $live || $live_small || $classes) {
            $rows = [
                ['order_type' => '点播课', 'payment' => $video],
                ['order_type' => '大班课', 'payment' => $live],
                ['order_type' => '小班课', 'payment' => $live_small],
                ['order_type' => '班级课', 'payment' => $classes],
            ];
        }

        return $rows;
    }
}
